*ALTERNATIVE DV ETHNORACIAL

clear all
use "ESS.dta" 


*1st alternative variable Ethnoracial: needs merging with ess7, because it's a variable that has less that 20% observations when considering the whole ESS (first to last round) and hence got cut when I implemented a loop cutting each variable with more than 80% missings.
 
 *Hence before running this, ensure ESS7.dta is in the same directory 
 
 merge m:1 essround idno cntry using "ESS7.dta", keepusing(smegbli smegbhw)

gen D_racea=2-smegbli  
gen D_raceb=2-smegbhw  
gen D=D_raceb


 label var D "Discriminatory attitudes"

 
 local y1 "D"
 local x1 "total_eduyrs"
 local z1 "Age Female Edu_mum  i.Country  i.essround"
 local z2 "Female  Edu_mum i.Country  i.essround"
 local IV "T r"
 local z1b "Age Age2 Female Edu_mum Africa Asia Muslim Jew i.Country  i.essround"
 local z2b "Female  Edu_mum i.Country Africa Asia Muslim Jew  i.essround"

 
quietly: reg  $y1   $x1  
est store Correlation
local N1=  e(N)
 
quietly:reg  $y1   $x1  $z1  if Monotonic==1, robust
est store OLS
local N2=  e(N)

 
drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


quietly:rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum Female r1 r2 r3 r4 r5 r6 r7 r8  c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2 [pweight= w] if Monotonic==1, first  baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Meanm:di%8.2fc r(mean)
drop w

reg  $y1  r 
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean) 
loc Meanm=trim("`Mean'")
di `Means'

quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum Female  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w

	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color   "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
	coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'") ) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" ))  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" ) ) ,     keep( total_eduyrs)   xscale(range(-0.2,0.2)) xlab(-0.2(.05)0.2, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Some races or ethnic groups are born lazy", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Ethnoracial attitudes}")  note("OLS, IV(all) and IV(strong) control for gender,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification: 1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)
graph export "AppendixB_3a.png", replace 
 
 
drop total_eduyrs
gen total_eduyrs=  total_eduyrs2
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


* 2nd alternative variable Ethnoracial


clear all
use "ESS.dta" 

 
g DD=.
replace DD=0 if Immigration_ethnic<=Immigration_same  
replace DD=1 if Immigration_ethnic>Immigration_same
replace DD=. if Africa==1 |  Asia==1 |   America==1 |  Other==1|  Muslim==1 |  Jew ==1 |  Minority==1 |  EEurope==1  
	
gen D=DD
  label var D "Discriminatory attitudes"
 
 local y1 "D"
 local y2 "PD" 
 local x1 "total_eduyrs"
 local z1 "Age Female Edu_mum  i.Country  i.essround"
 local z2 "Female  Edu_mum i.Country  i.essround"
 local z1b "Age Age2 Female Edu_mum Africa Asia   Muslim   i.Country  i.essround"
 local z2b "Female  Edu_mum i.Country Africa Asia    Muslim  i.essround"

 
*Discrimination


quietly: reg  $y1   $x1  
est store Correlation
local N1=  e(N)
 
quietly:reg  $y1   $x1  $z1  if Monotonic==1, robust
est store OLS
local N2=  e(N)

 
drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


quietly:rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum Female r1 r2 r3 r4 r5 r6 r7 r8  c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2 [pweight= w] if Monotonic==1, first  baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Meanm:di%8.2fc r(mean)
drop w

reg  $y1  r 
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean) 
loc Meanm=trim("`Mean'")
di `Means'

quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum Female  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w

	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color   "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
	coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'") ) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" ))  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" ) ) ,     keep( total_eduyrs)   xscale(range(-0.2,0.2)) xlab(-0.2(.05)0.2, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Prefer migrants of same race/ethnicity", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Ethnoracial attitudes}")  note("OLS, IV(all) and IV(strong) control for gender,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification: 1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)
graph export "AppendixB_3b.png", replace 

